package com.atguigu.myzhxy.controller;

import com.atguigu.myzhxy.pojo.Student;
import com.atguigu.myzhxy.service.StudentService;
import com.atguigu.myzhxy.util.MD5;
import com.atguigu.myzhxy.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/studentController")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //http://localhost:8080/sms/studentController/addOrUpdateStudent post
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@RequestBody Student student){
        if (student.getId()==null || student.getId()==0){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }
    //http://localhost:8080/sms/studentController/delStudentById delete
    @DeleteMapping("/delStudentById")
    public Result delStudentById(@RequestBody List<Integer> ids){
        boolean removeByIds = studentService.removeByIds(ids);
        return Result.ok(removeByIds);
    }
    //http://localhost:8080/sms/studentController/getStudentByOpr/1/3 get
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(@PathVariable Integer pageNo,
                                  @PathVariable Integer pageSize,
                                  Student student) {
     Page<Student> page=new Page<>(pageNo,pageSize);
     IPage<Student> iPage=studentService.getStudentByOpr(page,student);
     return Result.ok(iPage);
    }

}
